/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.photo;

import cn.ac.nercis.pes.community.component.dialog.MessageDialog;
import cn.ac.nercis.pes.community.component.dialog.MessageType;
import cn.ac.nercis.pes.community.model.dto.SizeDTO;
import cn.ac.nercis.pes.community.support.controls.JFXDialog;
import cn.ac.nercis.pes.community.utils.ControlUtils;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * 图片查看器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class PictureViewer {
    private final JFXDialog dialog;

    @Getter
    @Setter
    private String title;
    @Getter
    @Setter
    private File pictureFile;
    @Getter
    @Setter
    private StackPane dialogContainer;

    public PictureViewer() {
        dialog = new JFXDialog();
    }

    public void show(){
        try {
            this.render();
            this.dialog.show();
        }catch (IOException ignore){
            MessageDialog.builder()
                    .withContainer(dialogContainer)
                    .withType(MessageType.FAIL)
                    .withMessage("打开图片查看器失败！")
                    .build().show();
        }
    }

    private void render() throws IOException{
        this.dialog.setDialogContainer(dialogContainer);
        this.dialog.setOverlayClose(true);
        VBox page = new VBox();
        page.getStyleClass().add("pes-dialog-layout");
        this.renderTitle(page);
        this.renderPicture(page);
        this.dialog.setContent(page);
    }

    private void renderTitle(VBox page){
        var pane = new HBox();
        pane.getStyleClass().add("pes-layout-heading");
        pane.setAlignment(Pos.CENTER_LEFT);
        pane.setPadding(new Insets(0,10,0,20));
        var titleLabel = new Label(this.title);
        titleLabel.getStyleClass().add("page-dialog-title");
        titleLabel.setGraphic(IconUtils.getGlyph("picture-viewer-icon","page-dialog-icon"));
        titleLabel.setGraphicTextGap(5);

        var toolsBox = new HBox();
        toolsBox.setAlignment(Pos.CENTER_RIGHT);
        HBox.setHgrow(toolsBox, Priority.SOMETIMES);
        var closeButton = new Button();
        closeButton.getStyleClass().add("main-window-btn");
        closeButton.setGraphic(IconUtils.getGlyph("win-close","main-title-bar-close-icon"));
        closeButton.setTooltip(new Tooltip("关闭"));
        closeButton.setOnAction(e -> dialog.close());
        toolsBox.getChildren().add(closeButton);
        pane.getChildren().addAll(titleLabel,toolsBox);

        page.getChildren().add(pane);
    }

    private void renderPicture(VBox page) throws IOException {
        HBox pictureBox = new HBox();
        pictureBox.setAlignment(Pos.CENTER);
        VBox.setVgrow(pictureBox,Priority.SOMETIMES);
        ScrollPane scrollPane = new ScrollPane();
        StackPane stackPane = new StackPane();

        Image image = new Image(new ByteArrayInputStream(FileUtils.readFileToByteArray(pictureFile)));
        ImageView imageView = new ImageView(image);
        imageView.getStyleClass().add("picture-viewer");
        final double maxWidth = dialogContainer.getWidth() - 100;
        final double maxHeight = dialogContainer.getHeight() - 100;
        SizeDTO imageSize = ControlUtils.getPhotoSize(image,(int)maxWidth,(int)maxHeight);
        scrollPane.setPrefHeight(imageSize.height());
        imageView.setFitHeight(imageSize.height());
        scrollPane.setPrefWidth(imageSize.width());
        imageView.setFitWidth(imageSize.width());

        stackPane.setPrefHeight(scrollPane.getPrefHeight());
        stackPane.setPrefWidth(scrollPane.getPrefWidth());
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setPannable(true);
        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        StackPane.setAlignment(imageView,Pos.CENTER);

        stackPane.getChildren().add(imageView);
        scrollPane.setContent(stackPane);
        pictureBox.getChildren().add(scrollPane);
        page.getChildren().add(pictureBox);
    }


}